package cd.cdyb.sms.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class ExcelUtils {

    public static List<List<String>> importExcel(File file) throws IOException {
        List<List<String>> data = new ArrayList<>();
        InputStream inputStream = new FileInputStream(file);
        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            List<String> rowData = new ArrayList<>();
            Iterator<Cell> cellIterator = row.iterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                rowData.add(getCellValueAsString(cell));
            }
            data.add(rowData);
        }

        workbook.close();
        return data;
    }

    private static String getCellValueAsString(Cell cell) {
        switch (cell.getCellTypeEnum()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            default:
                return "";
        }
    }

    public static void main(String[] args) throws IOException {
fun2();
    }

    private static void fun1() throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\sms\\s1.xlsx");
        List<List<String>> lists = importExcel(file);

        File file2 = new File("C:\\Users\\Administrator\\Desktop\\sms\\s2.xlsx");
        List<List<String>> lists2 = importExcel(file2);
        Map<String, Integer> map = new HashMap<>();
        Map<String, Integer> map1 = new HashMap<>();

        for (List<String> list : lists) {
            String phone = list.get(1);
            String status = list.get(2);
            if ("发送成功".equals(status)){
                if (map.containsKey(phone)) {
                    Integer integer = map.get(phone);
                    integer = integer +1;
                    map.put(phone, integer);
                } else {
                    map.put(phone, 1);
                }
            }
        }
        for (List<String> list : lists2) {
            String phone = list.get(0);
            String status = list.get(3);
            if ("1.0".equals(status)){
                if (map.containsKey(phone)) {
                    Integer integer = map.get(phone);
                    integer = integer -1;
                    map.put(phone, integer);
                }
            }
        }

        map.forEach((s, integer) -> {
            if(integer > 0){
                System.out.println("异常手机号" + s + ",剩余次数" + integer);

            }
        });
    }


    private static void fun2() throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\sms\\ss5.xlsx");
        List<List<String>> lists = importExcel(file);

        File file2 = new File("C:\\Users\\Administrator\\Desktop\\sms\\ss6.xlsx");
        List<List<String>> lists2 = importExcel(file2);
        Map<String, Integer> map = new HashMap<>();
        Map<String, Integer> map1 = new HashMap<>();

        for (List<String> list : lists2) {

            String phone = list.get(0);
            String status = list.get(1);
            if ("1.0".equals(status)){
                if (map.containsKey(phone)) {
                    Integer integer = map.get(phone);
                    integer = integer +1;
                    map.put(phone, integer);
                } else {
                    map.put(phone, 1);
                }
            }

        }
        for (List<String> list : lists) {
            String phone = list.get(1);
            String status = list.get(2);
            if ("发送成功".equals(status)){
                if (map.containsKey(phone)) {
                    Integer integer = map.get(phone);
                    integer = integer -1;
                    map.put(phone, integer);
                }
            }
        }

        map.forEach((s, integer) -> {
            if(integer > 0){
                System.out.println("异常手机号" + s + ",剩余次数" + integer);

            }
        });
    }
}
